package api

import (
	"gateway/basic/config"
	__ "gateway/basic/proto"
	"gateway/handler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddCar(c *gin.Context) {
	var req request.AddCarReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "参数错误",
		})
		return
	}
	_, err := config.DacheClient.CarAdd(c, &__.CarAddReq{
		UserId:        int64(req.UserId),
		CarColor:      req.CarColor,
		CarType:       req.CarType,
		CarBrand:      req.CarBrand,
		DrivingNumber: req.DrivingNumber,
		CarNumber:     req.CarNumber,
		SeatNum:       int64(req.SeatNum),
		CarImg:        req.CarImg,
		CarStatus:     int64(req.CarStatus),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "客户端连接失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "车辆信息添加成功",
		"data": nil,
	})
}

func GetCarList(c *gin.Context) {
	var req request.GetCarListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "参数错误",
		})
		return
	}
	car, err := config.DacheClient.GetCarList(c, &__.GetCarListReq{
		CarType: req.CarType,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "客户端连接失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "车辆信息展示成功",
		"data": car,
	})
}
